package Frames;

import Controllers.FindStore_Controller;
import DAO.FindStore_Dao;
import DAO.javaconnect2;
import Exceptions.DaoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.sql.*;
import net.proteanit.sql.DbUtils;

public class FindStore_Frame extends javax.swing.JFrame {
    
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    public FindStore_Frame() {
        initComponents();
        conn = javaconnect2.connectDB();
        updadeTable();
    }
    
    public void updadeTable(){
        try{
        String sql = "select * from stores";
        pst= conn.prepareStatement(sql);
        rs=pst.executeQuery();
        jTable_Stores.setModel(DbUtils.resultSetToTableModel(rs));
        
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Cancel = new javax.swing.JButton();
        Search = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jStoreName2 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable_Stores = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Cancel.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
        Cancel.setText("Home");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        Search.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
        Search.setText("Search");
        Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel1.setText("Find Store");
        jLabel1.setName(""); // NOI18N

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
        jLabel2.setText("Store Name");

        jStoreName2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jStoreName2ActionPerformed(evt);
            }
        });

        jTable_Stores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable_Stores.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_StoresMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable_Stores);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(175, 175, 175)
                        .addComponent(jLabel2)
                        .addGap(107, 107, 107)
                        .addComponent(jStoreName2, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Search)
                            .addGap(45, 45, 45)
                            .addComponent(Cancel)
                            .addGap(80, 80, 80))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(32, 32, 32)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 703, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1)))))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jStoreName2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Search)
                    .addComponent(Cancel))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        this.dispose();
        MainFrame x = new MainFrame();

        x.setVisible(true);
    }//GEN-LAST:event_CancelActionPerformed

    private void SearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchActionPerformed
        
        FindStore_Controller controller = new FindStore_Controller(new FindStore_Dao());
                
        try {
            boolean findstore = false;
            try{
                findstore = controller.validateFindStore(jStoreName2.getText());
            }catch (DaoException ex) {
                Logger.getLogger(StoreEntry_Frame.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (findstore==true){
                //JOptionPane.showMessageDialog(null, "Store was found");
                String find = jStoreName2.getText();
                this.dispose();
                StoreData_Frame x = new StoreData_Frame();
                StoreData_Frame.jStoreNm.setText(this.jStoreName2.getText());
                try{
                    String query ="select * from stores where storename=?";
                    pst = conn.prepareStatement(query);
                    pst.setString(1, find);
                    rs = pst.executeQuery();

                    while(rs.next()){
                    String add1 = rs.getString("address");
                    StoreData_Frame.jAddress.setText(add1);
                    String add2 = rs.getString("city");
                    StoreData_Frame.jCity.setText(add2);
                    String add3 = rs.getString("storephone");
                    StoreData_Frame.jStorePhone.setText(add3);
                    String add4 = rs.getString("floors");
                    StoreData_Frame.jFloors.setText(add4);
                    String add5 = rs.getString("seats");
                    StoreData_Frame.jSeats.setText(add5);
                    String add6 = rs.getString("parking");
                    StoreData_Frame.jParking.setText(add6);
                    String add7 = rs.getString("parkingspots");
                    StoreData_Frame.jParkingSpots.setText(add7);
                    String add8 = rs.getString("daysopen");
                    StoreData_Frame.jDaysOpen.setText(add8);
                    String add9 = rs.getString("storename");
                    StoreData_Frame.jStoreNm.setText(add9);
                
            }
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e);
                }
                 x.setVisible(true);
            }
           else {
                JOptionPane.showMessageDialog(null, "You typed something wrong try again");
           }
           
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Error" + e);
        }
        
    }//GEN-LAST:event_SearchActionPerformed

    private void jStoreName2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jStoreName2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jStoreName2ActionPerformed

    private void jTable_StoresMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_StoresMouseClicked
        
         StoreData_Frame x = new StoreData_Frame();
                         
        try{
            int row = jTable_Stores.getSelectedRow();
            String Table_click = (jTable_Stores.getModel().getValueAt(row, 0).toString());
            String query ="select * from stores where storename='"+Table_click+"' "; 
            pst = conn.prepareStatement(query);
            rs = pst.executeQuery();
            if(rs.next()){
                String add1 = rs.getString("address");
                StoreData_Frame.jAddress.setText(add1);
                String add2 = rs.getString("city");
                StoreData_Frame.jCity.setText(add2);
                String add3 = rs.getString("storephone");
                StoreData_Frame.jStorePhone.setText(add3);
                String add4 = rs.getString("floors");
                StoreData_Frame.jFloors.setText(add4);
                String add5 = rs.getString("seats");
                StoreData_Frame.jSeats.setText(add5);
                String add6 = rs.getString("parking");
                StoreData_Frame.jParking.setText(add6);
                String add7 = rs.getString("parkingspots");
                StoreData_Frame.jParkingSpots.setText(add7);
                String add8 = rs.getString("daysopen");
                StoreData_Frame.jDaysOpen.setText(add8);
                String add9 = rs.getString("storename");
                StoreData_Frame.jStoreNm.setText(add9);
             
            }
       }catch(Exception e){
            
            JOptionPane.showMessageDialog(null, e);
            
        } x.setVisible(true);
    }//GEN-LAST:event_jTable_StoresMouseClicked

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JButton Search;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    public static javax.swing.JTextField jStoreName2;
    private javax.swing.JTable jTable_Stores;
    // End of variables declaration//GEN-END:variables
}
